package com.demo.boss.server.impl;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.boss.common.constants.SysConst;
import com.demo.boss.common.constants.type.SysException;
import com.demo.boss.common.enums.PublicStatus;
import com.demo.boss.common.enums.SysCode;
import com.demo.boss.entity.PayProduct;
import com.demo.boss.mapper.PayProductMapper;
import com.demo.boss.models.PageRequest;
import com.demo.boss.models.request.ProductRequest;
import com.demo.boss.models.vo.DicItemVo;
import com.demo.boss.server.ProductService;
import com.demo.boss.tools.RedisHelper;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private PayProductMapper productMapper;

    @Resource
    private RedisHelper redisHelper;
    @Override
    public IPage<PayProduct> pageList(PageRequest pageRequest) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("edit_time");
        return productMapper.selectPage(Page.of(pageRequest.getPageNo(), pageRequest.getPageSize()), wrapper);
    }

    @Override
    public boolean productAdd(ProductRequest productRequest) {
        // 验证 产品编号是否存在
        QueryWrapper<PayProduct> codeWrapper = new QueryWrapper<>();
        codeWrapper.eq("product_code",productRequest.getCode());
        Long count = productMapper.selectCount(codeWrapper);
        if (count>0){
            throw new SysException(SysCode.PAYMENT_PRODUCT_CODE_EXISTS_ERROR);
        }
        // 验证 产品名称是否存在
        QueryWrapper<PayProduct> nameWrapper = new QueryWrapper<>();
        nameWrapper.eq("product_name",productRequest.getName());
        count = productMapper.selectCount(nameWrapper);
        if (count>0){
            throw new SysException(SysCode.PAYMENT_PRODUCT_NAME_EXISTS_ERROR);
        }
        // 正常添加信息
        PayProduct payProduct = new PayProduct();
        payProduct.setId(IdWorker.get32UUID());
        payProduct.setProductCode(productRequest.getCode());
        payProduct.setProductName(productRequest.getName());
        payProduct.setStatus(PublicStatus.ENABLE.name());
        payProduct.setEditTime(new Date());
        payProduct.setRemark("创建新的支付产品");
        int insert = productMapper.insert(payProduct);

        // 添加数据成功，再添加支付产品到 redis
        JSONObject jsObject = new JSONObject();
        jsObject.set("code",productRequest.getCode());
        jsObject.set("name",productRequest.getName());
        redisHelper.addListElement(SysConst.PAY_PRODUCT_LIST,jsObject.toJSONString(0)); //sObject.toJSONString(0) 不用字符隔开
        return insert > 0;
    }
        // [1-10] --> [0-9]
    @Override
    public List<DicItemVo> queryProductDic(PageRequest pageRequest) {
        Integer start = (pageRequest.getPageNo() - 1) * pageRequest.getPageSize();
        Integer end = pageRequest.getPageNo() * pageRequest.getPageSize() -1 ;
        List<String> productJsonArray = redisHelper.getListElement(SysConst.PAY_PRODUCT_LIST, start, end);

        List<DicItemVo> list = new ArrayList<>();
//        {"code": "樊振东","name": "全运会冠军"}
        for (String productJson : productJsonArray) {
            list.add(JSONUtil.toBean(productJson, DicItemVo.class));
        }
        return list;
    }

    @Override
    public List<PayProduct> matchProductName(String productName) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.select("product_code","product_name")
                .like("product_name",productName)
                .orderByAsc("product_code");
        return productMapper.selectList(wrapper);
    }
}
